package com.pj.wisdomlogistics.utils;

import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.ui.activity.BaseActivity;
import com.willy.ratingbar.BaseRatingBar;
import com.willy.ratingbar.ScaleRatingBar;

/**
 * Created by WuFan on 2017/10/20.
 */

public class ShowPop {
    private static int rating;
    public static listener listener;

    public static void showTurnDown(final BaseActivity mActivity , final int state , final View mView){
        View contentView = LayoutInflater.from(mActivity).inflate(R.layout.pop_turndown, null);
        int screenWidth = ScreenUtils.getScreenWidth(mActivity);
        int screenHeight = ScreenUtils.getScreenHeight(mActivity);
        final PopupWindow mPopWindow = new PopupWindow(contentView,
                (int) (screenWidth * 0.9), LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        mPopWindow.setTouchable(true);
        mPopWindow.setBackgroundDrawable(new ColorDrawable());
        backgroundAlpha(mActivity , 0.3f);
        Button mBtSend = (Button) contentView.findViewById(R.id.bt_send);
        final TextView mTvTitle = (TextView) contentView.findViewById(R.id.tv_title);
        final EditText mEditText = (EditText) contentView.findViewById(R.id.et_turndown);
        final ScaleRatingBar mRatingBar = (ScaleRatingBar) contentView.findViewById(R.id.simpleRatingBar);
        if(state == 101){
            mRatingBar.setVisibility(View.VISIBLE);
            mRatingBar.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
                @Override
                public void onRatingChange(BaseRatingBar mBaseRatingBar, float mV) {
                    rating = (int) mV;
                }
            });

            mTvTitle.setText("评价反馈");
            mEditText.setHint("请输入评价反馈");
        }

        mBtSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //驳回提交
                mPopWindow.dismiss();
                String text = mEditText.getText().toString().trim();
                listener.showTurnDown(state , text , rating);
            }
        });
        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(mActivity , 1f);
            }
        });

        mPopWindow.showAtLocation(mView, Gravity.NO_GRAVITY, (int) (screenWidth * 0.05), (int) (screenHeight * 0.3));
    }

    public static void backgroundAlpha(BaseActivity mActivity, float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }

    public interface listener{
        void showTurnDown(int state , String text , int rating);
    }

    public static void setListner(listener mListener){
        listener = mListener;
    }
}
